package com.hh.service.service.impl.pagemanagement;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hh.common.constants.Constants;
import com.hh.common.exception.HhException;
import com.hh.common.model.pagemanagement.JingangArea;
import com.hh.common.response.JingangAreaResponse;
import com.hh.common.token.FrontTokenComponent;
import com.hh.common.utils.SecurityUtil;
import com.hh.common.vo.JingangAreaVo;
import com.hh.service.dao.JingangAreaDao;
import com.hh.service.service.SystemAttachmentService;
import com.hh.service.service.pagemanagement.JingangAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class JingangAreaServiceImpl extends ServiceImpl<JingangAreaDao, JingangArea> implements JingangAreaService {

    @Autowired
    private SystemAttachmentService systemAttachmentService;

    @Autowired
    private FrontTokenComponent tokenComponet;

    @Override
    public List<JingangAreaVo> getJingangAreaList(boolean isMer) {
        String merId = Constants.PLATFORM_MER_ID;
        if(isMer){
            merId = SecurityUtil.getMerLoginUserVo().getUser().getMerId();
        }
        LambdaQueryWrapper<JingangArea> objectLambdaQueryWrapper = Wrappers.lambdaQuery();
        objectLambdaQueryWrapper.eq(JingangArea::getMerId,merId);
        objectLambdaQueryWrapper.orderByAsc(JingangArea::getSort);
        List<JingangArea> jingangAreas = this.baseMapper.selectList(objectLambdaQueryWrapper);
        List<JingangAreaVo> list = new ArrayList<>();
        if(jingangAreas.size() > 0){
            for (JingangArea jingangArea : jingangAreas) {
                JingangAreaVo jingangAreaResponse = new JingangAreaVo();
                BeanUtil.copyProperties(jingangArea,jingangAreaResponse);

                if(jingangArea.getType() == 1){
                    jingangAreaResponse.setCateId(Integer.valueOf(jingangArea.getLinkParam()));
                }else if(jingangArea.getType() == 5) {
                    jingangAreaResponse.setSpecialZone(jingangArea.getLinkParam());
                }else {
                    jingangAreaResponse.setPagePath(jingangArea.getLinkParam());
                }

                list.add(jingangAreaResponse);
            }
        }

        return list;

    }

    @Override
    public void jingangAreaSave(JingangAreaVo param) {
        if (param.getType().equals(1) && param.getCateId() == null) {
            throw new HhException("请选择商品分类");
        }
        if (param.getType().equals(5) && StrUtil.isBlank(param.getSpecialZone())) {
            throw new HhException("请选择专区或推荐位");
        }
        if (param.getType().equals(10) && StrUtil.isBlank(param.getPagePath())) {
            throw new HhException("请填写页面路径");
        }
        JingangArea jingangArea = new JingangArea();
        BeanUtil.copyProperties(param,jingangArea);
        if(param.getType().equals(1)){
            jingangArea.setLinkParam(param.getCateId().toString());
        }else if(param.getType().equals(5)){
            jingangArea.setLinkParam(param.getSpecialZone());
        }else {
            jingangArea.setLinkParam(param.getPagePath());
        }
        // 处理图片链接
        jingangArea.setImgUrl(this.systemAttachmentService.clearPrefix(param.getImgUrl()));
        if (null != param.getId()) {
            // 更新
            JingangArea jingangArea1 = getById(param.getId());
            BeanUtil.copyProperties(jingangArea,jingangArea1);
            updateById(jingangArea1);
        } else {
            // 新增
            save(jingangArea);
        }
    }

    @Override
    public List<JingangAreaResponse> getFrontJingangAreaList() {
        String merId = tokenComponet.getMerId();
        List<JingangAreaResponse> list = this.baseMapper.getFrontJingangAreaList(merId);
        return list;
    }

}
